package cn.snmp.mutual.util;

import java.util.LinkedList;
import java.util.ResourceBundle;
import java.util.Vector;

import cn.snmp.mutual.common.tools.XmlFile;
import lombok.extern.slf4j.Slf4j;

import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.data.XmlService;

/**
 * @author dengkn
 * 工具类集合
 */
@Slf4j
public class Utilities {

    public static String[] stringSplit(String value, char c) {
        try {
            LinkedList e = new LinkedList();
            String splitString = new String(value);

            for (int result = splitString.indexOf(c); result != -1; result = splitString.indexOf(c)) {
                String j = new String(splitString.substring(0, result));
                splitString = splitString.substring(result + 1);
                e.add(j);
            }
            e.add(splitString);
            String[] arg6 = new String[e.size()];

            for (int arg7 = 0; arg7 < e.size(); ++arg7) {
                arg6[arg7] = (String) e.get(arg7);
            }

            return arg6;
        } catch (Exception arg5) {
            DumpUtil.debug(arg5);
            return null;
        }
    }

    public static void copyPropreties(ObjService osSrc, ObjService osDes) {
        for (Object key : osSrc.getStoreValueMap().keySet()) {
            osDes.setValue((String) key, osSrc.getStringValue((String) key));
        }
    }

    public static ObjService getObjServiceFromXmlFile(String xmlFileName) {
        try {
            XmlFile e = new XmlFile(xmlFileName);
            XmlService xmlService = e.node2XmlService(e.getRootNode());
            ObjService rootNode = XmlService.xmlService2ObjService(xmlService);
            return rootNode;
        } catch (Exception arg3) {
            DumpUtil.debug("Parse XML File " + xmlFileName + " Error.");
            DumpUtil.debug(arg3);
            return null;
        }
    }

    public static String getFilePatch(String strFullName, char spliter) {
        String[] tmp = stringSplit(strFullName, spliter);
        String path = "";

        for (int i = 0; i < tmp.length - 1; ++i) {
            path = path + tmp[i] + spliter;
        }

        return path;
    }

    public static void dumpData(ObjService object) {
        DumpUtil.debug("############################# Utilities.dumpData() #############################");
        DumpUtil.debug(object);
        DumpUtil.debug("############################# Utilities.dumpData() #############################");

    }

    public static boolean isIndexNode(ObjService mibNode) {
        try {
            String e = mibNode.getStringValue("IsIndex");
            return e.equalsIgnoreCase("True");
        } catch (Exception arg1) {
            DumpUtil.debug(arg1);
            return true;
        }
    }

    public static int porpetySize(ObjService object) {
        return object.getStoreValueMap().size();
    }

    public static boolean isStatusNode(ObjService mibNode) {
        try {
            String e = mibNode.getStringValue("IsStatus");
            return e.equalsIgnoreCase("True");
        } catch (Exception arg1) {
            DumpUtil.debug(arg1);
            return true;
        }
    }

    public static boolean isHideNode(ObjService mibNode) {
        try {
            String e = mibNode.getStringValue("IsHide");
            return e.equalsIgnoreCase("True");
        } catch (Exception arg1) {
            DumpUtil.debug(arg1);
            return true;
        }
    }

    public static String getResource(ResourceBundle boundle, String strResourceString, String strReplaceHead,
                                     String strNoReplaceHead) {
        return "";
    }

    public static String getResource(ResourceBundle boundle, String lable) {
        try {
            String lableResource = boundle.getString(lable);
            return lableResource;
        } catch (Exception arg3) {
            return lable;
        }
    }

    public static ObjService getChild(ObjService object, String className, String attribute, String value) {
        for (int i = 0; i < object.objectSize(className); ++i) {
            if (object.objectAt(className, i).getStringValue(attribute).equals(value)) {
                return object.objectAt(className, i);
            }
        }

        return null;
    }

    public static void addAllVoffspring(ObjService osResource, LinkedList allVoffspring) {
        allVoffspring.add(osResource);
        LinkedList LinkedList = osResource.getObjLinkListVecs();

        for (int i = 0; i < LinkedList.size(); ++i) {
            LinkedList childLinkedList = (LinkedList) LinkedList.get(i);

            for (int j = 0; j < childLinkedList.size(); ++j) {
                ObjService childObjResource = (ObjService) childLinkedList.get(j);
                addAllVoffspring(childObjResource, allVoffspring);
            }
        }

    }

    public static ObjService getChildInObjService(ObjService osNode, String className, String attribute, String value) {
        ObjService child = null;
        LinkedList voffspring = new LinkedList();
        addAllVoffspring(osNode, voffspring);

        for (int i = 0; i < voffspring.size(); ++i) {
            ObjService node = (ObjService) voffspring.get(i);
            String name = node.getClsName();
            if (name.equals(className)) {
                String v = node.getStringValue(attribute);
                if (v.equals(value)) {
                    child = node;
                }
            }
        }

        voffspring.clear();
        return child;
    }

    public static int ipAddressStringToInt(String strIPAddress) {
        int intIPAddress = 0;
        if (strIPAddress == null) {
            return intIPAddress;
        } else {
            DumpUtil.debug("the strIPAddress is : " + strIPAddress);
            String[] subIpAddress = new String[4];

            int i;
            for (i = 0; i < 3; ++i) {
                int s = strIPAddress.indexOf(".");
                subIpAddress[i] = strIPAddress.substring(0, s);
                strIPAddress = strIPAddress.substring(s + 1);
            }

            subIpAddress[3] = strIPAddress;
            if (subIpAddress.length != 4) {
                return intIPAddress;
            } else {
                for (i = 0; i < 4; ++i) {
                    String arg7 = subIpAddress[i];
                    int j = Integer.decode(arg7).intValue();
                    int power = 1;

                    for (int k = 4 - i - 1; k > 0; --k) {
                        power *= 256;
                    }

                    intIPAddress += j * power;
                }

                return intIPAddress;
            }
        }
    }

    public static String intToIPAddressString(int intIPAddress) {
        StringBuilder strIpAddress = new StringBuilder();
        log.debug("the strIPAddress is : " + intIPAddress);
        for (int i = 0; i < 4; ++i) {
            int isub = intIPAddress << 8 * i >>> 24;
            if (isub < 0) {
                isub += 256;
            }
            strIpAddress.append(isub);
            if (i != 3) {
                strIpAddress.append(".");
            }
        }
        log.debug("the strIpAddress is : " + strIpAddress);
        return strIpAddress.toString();
    }
	
/*	public static DefaultNeObject getSourceDefaultNeObject(TestResultBean testResultBean) {
		DefaultNeObject ne = new DefaultNeObject();
		ne.setProperty("snmpVersion", testResultBean.getSrcsnmpVersion());
		ne.setProperty("WRITECOM", testResultBean.getSrcWritecom());
		ne.setProperty("READCOM", testResultBean.getSrcReadcom());
		ne.setProperty("TIMEOUT", testResultBean.getSrcTimeout());
		ne.setProperty("PORT", String.valueOf(testResultBean.getSourceNeSnmpPort()));
		ne.setProperty("IPADDRESS", testResultBean.getSourceNeManagementIp());
		ne.setProperty("RETRY", testResultBean.getSrcRetry()); 
		ne.setProperty("V3", testResultBean.getSrcV3());
		return ne;
	}
	
	public static DefaultNeObject getDestDefaultNeObject(TestResultBean testResultBean) {
		DefaultNeObject ne = new DefaultNeObject();
		ne.setProperty("snmpVersion", testResultBean.getDestsnmpVersion());
		ne.setProperty("WRITECOM", testResultBean.getDestWritecom());
		ne.setProperty("READCOM", testResultBean.getDestReadcom());
		ne.setProperty("TIMEOUT", testResultBean.getDestTimeout());
		ne.setProperty("PORT", String.valueOf(testResultBean.getDestinationNeSnmpPort()));
		ne.setProperty("IPADDRESS", testResultBean.getDestinationNeManagementIp());
		ne.setProperty("RETRY", testResultBean.getDestRetry()); 
		ne.setProperty("V3", testResultBean.getDestV3());
		return ne;
	}*/

    /**
     * 使用指定的字符将字符串分割成字符串列表
     *
     * @param value 待分割的字符串
     * @param c     指定的字符
     * @return 分割后的字符串列表
     */
    public static String[] StringSplit(String value, char c) {
        try {
            Vector subStringVector = new Vector();
            String splitString = new String(value);

            for (int i = splitString.indexOf((int) c); i != -1; i = splitString.indexOf((int) c)) {
                String subString = new String(splitString.substring(0, i));
                splitString = splitString.substring(i + 1);
                subStringVector.add(subString);
            }

            subStringVector.add(splitString);
            String[] result = new String[subStringVector.size()];
            for (int j = 0; j < subStringVector.size(); j++) {
                result[j] = (String) subStringVector.get(j);
            }

            subStringVector.removeAllElements();
            return result;
        } catch (Exception e) {
            DumpUtil.debug(e);
            return null;
        }
    }
}